package com.ease.document.flyingsaucer;

import java.io.OutputStream;

import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

/**
 * <a herf="http://www.javaeye.com/topic/509417" />
 * 
 * @author downpour
 * 
 */
public class XhtmlRendererUtil {

	public static void process(OutputStream outputStream, String documentString) {
		ITextRenderer renderer = new ITextRenderer();
		renderer.setDocumentFromString(documentString);
		// 解决中文支持问题
		ITextFontResolver fontResolver = renderer.getFontResolver();
		try {
			// fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			// 解决图片的相对路径问题
			renderer.getSharedContext().setBaseURL("file:/D:/Work/Demo2do/Yoda/branch/Yoda%20-%20All/conf/template/");
			renderer.getSharedContext().setUserAgentCallback(new HttpUrlUserAgent(renderer.getOutputDevice()));

			renderer.layout();
			// renderer.createPDF(outputStream);
			renderer.createPDF(outputStream, true);
		} catch (Exception e) {

		}
		// return outputStream;
		// this.inputStream = new ByteArrayInputStream(outputStream.toByteArray());
	}

}
